package com.zl;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Protocol;

/**
 * 管道操作
 */
public class PipeLine {
    @Test
    public  void  test(){
        //创建连接池
        JedisPool jedisPool = new JedisPool(new GenericObjectPoolConfig(), "127.0.0.1", 6379, Protocol.DEFAULT_TIMEOUT, "foobared", 3);
        Jedis jedis = jedisPool.getResource();

        /**
         * 普通操作执行100000次+1操作
         */
        Long start = System.currentTimeMillis();

        for (int i = 0; i < 100000; i++) {
            jedis.incr("i");
        }
        Long end = System.currentTimeMillis();
        System.out.println("普通耗时" + (end - start));


        /**
         * pipeline 一次性的执行所有的命令
         */
        start = System.currentTimeMillis();
        Pipeline pipeline = jedis.pipelined();
        for (int i = 0; i < 100000; i++) {
            pipeline.incr("i");
        }
        end = System.currentTimeMillis();
        System.out.println("管道耗时" + (end - start));
        jedis.close();
        jedisPool.close();

    }

}
